package yxy.game.pm2.active.command.warlog;

import yxy.apple.logger.Logger;
import yxy.cherry.data.bean.actsecretcharge;
import yxy.cherry.data.bean.actsecretreward;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_征战日志;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.subordinateclass.zztask;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.task.Task;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActWarLogInfoRequest;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * 征战日志信息
 * @author Dell
 *
 */
@MessageType(proto = Command.ActWarLogInfo, classtype = ActiveMessage.ActWarLogInfoRequest.class)
public class CMD_Active_征战日志_Info extends Command<ActWarLogInfoRequest> {

	private static final Logger logger = Logger.create(CMD_Active_征战日志_Info.class);
	
	@Override
	public void execute(MessageSource messageSource, ActWarLogInfoRequest request) throws Exception {
		String playercode = messageSource.getTag();
		ActiveMessage.ActWarLogInfoResponse.Builder response = ActiveMessage.ActWarLogInfoResponse.newBuilder();
		
		ActiveModule_征战日志 am = ActiveModule_征战日志.get(playercode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		if(am.getRunday() < 7) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		

		//经验奖励
		Map<String, actsecretreward> asrmap = ActDataCenter.getAllData(actsecretreward.class, playercode);
		for(String key : asrmap.keySet()) {
			actsecretreward asr = asrmap.get(key);
			ActiveMessage.WarLogRewards.Builder expreward = ActiveMessage.WarLogRewards.newBuilder();
			expreward.setId(asr.getId());
			expreward.setCondition(asr.getCondition());
			
			ArrayList<Currency> curs = (ArrayList<Currency>) Currency.create(asr.getRewardtype(), asr.getRewardid(), asr.getRewardnum());
			Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
			expreward.addAllCur(items);
			
			int canget = am.getZzrewards().contains(asr.getId()) ? DrawStatus.hadget.getStatus() : 
					am.getZzexp() >= asr.getCondition() ? DrawStatus.canget.getStatus() : DrawStatus.cannotget.getStatus(); 
			expreward.setCanget(canget);
			
			response.addExprewards(expreward.build());
		}
		
		//充值奖励
		Map<String, actsecretcharge> asccmap = ActDataCenter.getAllData(actsecretcharge.class, playercode);
		for(String key : asccmap.keySet()) {
			actsecretcharge asc = asccmap.get(key);
			ActiveMessage.WarLogRewards.Builder payreward = ActiveMessage.WarLogRewards.newBuilder();
			payreward.setId(asc.getId());
			payreward.setCondition(asc.getCondition());
			
			ArrayList<Currency> curs = (ArrayList<Currency>) Currency.create(asc.getRewardtype(), asc.getRewardid(), asc.getRewardnum());
			Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
			payreward.addAllCur(items);
			
			int canget = am.getPayrewards().contains(asc.getId()) ? DrawStatus.hadget.getStatus() : 
					am.getPayamount() >= asc.getCondition() ? DrawStatus.canget.getStatus() : DrawStatus.cannotget.getStatus(); 
			payreward.setCanget(canget);
			
			response.addPayrewards(payreward.build());
		}
		
		response.setPayamount(am.getPayamount());
		response.setZzexp(am.getZzexp());
		response.setEndtime(am.getEnd().getMillis());
		
		//任务
		HashMap<String, zztask> zztaskmap = am.getRunningcodetaskmap();
		for (String zztaskcode : zztaskmap.keySet()) {
			zztask zztask = zztaskmap.get(zztaskcode);
			ActiveMessage.WarLogTaskInfo.Builder taskinfo = ActiveMessage.WarLogTaskInfo.newBuilder();
			taskinfo.setCode(zztask.getTaskcode());
			taskinfo.setTaskid(zztask.getTaskid());
			
			Task task = Task.get(playercode, zztaskcode);
			int canget = task.isReward() ? DrawStatus.hadget.getStatus() : task.isFinish() ? DrawStatus.canget.getStatus() : DrawStatus.cannotget.getStatus();
			taskinfo.setProgress(task.getProgress());
			taskinfo.setCanget(canget);
			
			response.addTaskinfo(taskinfo.build());
		}
		
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActWarLogInfoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
